package org.madgecko.basic.controller;

import org.madgecko.basic.model.ExtReturn;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/system")
public class SystemController {


//	@RequestMapping(value="/upload", method= RequestMethod.POST)
//	public void upload(MultipartFile file, HttpServletRequest req, HttpServletResponse resp){
//		String tempFullPath = req.getSession().getServletContext().getRealPath("/") + "/resources/temp/";
//		try {
//			String orgName = file.getOriginalFilename();
//			String extension = FilenameUtils.getExtension(orgName);
//			String storeName = String.valueOf(new Date().getTime()) + "." + extension;
//			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(tempFullPath + storeName));
//			ExtReturn er = new ExtReturn(true);
//			resp.setContentType("text/html;charset=UTF-8");
//			resp.getWriter().write(JsonUtil.getInstance().obj2json(er));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

	@ResponseBody
	@RequestMapping(value = "/countTmpFile", produces={"application/json; charset=UTF-8"})
	public Object countTmpFile(HttpServletRequest req){
		//临时文件
		String tmpFullPath = req.getSession().getServletContext().getRealPath("/") + "/resources/temp/";
		File tmpFolder = new File(tmpFullPath);
		//未被使用的附件
		Map<String, Object> countMap = new HashMap<String, Object>();
		countMap.put("tmpFile", tmpFolder.list().length);
		return new ExtReturn(true, countMap);
	}

	@ResponseBody
	@RequestMapping(value = "/cleanTmpFile", produces={"application/json; charset=UTF-8"})
	public Object cleanTmpFile(HttpServletRequest req){
		String tmpFullPath = req.getSession().getServletContext().getRealPath("/") + "/resources/temp/";
		File tmpFolder = new File(tmpFullPath);
		for(File f : tmpFolder.listFiles()){
			f.delete();
		}
		return new ExtReturn(true);
	}

}
